/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Dao;

import com.lcfgly.Database.ShopUsermenu;
import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import com.lcfgly.Interface.GetCacheMark;
import com.lcfgly.Interface.UpdateCacheMark;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * Usermenu表操作
 * @author lcfgly
 */
@Repository
public class UserMenuImple implements Serializable{

    private final String table="shop_usermenu";
    private final String sqlheader="SELECT id,userid,typename,isnews,sort FROM shop_usermenu ";
    
    @Autowired
        @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    
    /**
     * 按userid和id 更新
     * @param arg0
     * @param userid
     * @return
     * @throws SQLException 
     */
    @UpdateCacheMark(value={"shopusermenubyid_${1}","shopusermenuSimpleByUserid_${0}"})
    public int updateWithUserid(ShopUsermenu arg0,int userid) throws SQLException {
        return dataFactory.update(table,arg0, "id=? AND userid =?", arg0.getId(),userid);
    }
    
    /**
     * 按userid查询
     * @param userid
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopusermenubyid_${0}")
    public List<ShopUsermenu> findByUserid(int userid) throws SQLException
    {
        String sql=sqlheader+" WHERE userid=?";
        return dataFactory.rows(ShopUsermenu.class,sql, userid);
    }
    
    /**
     * 按userid查询，返回简易的信息 id(int),typename(String),isnews(Short)
     * @param userid
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopusermenuSimpleByUserid_${0}")
    public List<Map> findSimpleByUserid(int userid) throws SQLException
    {
        String sql="SELECT id,typename,isnews FROM "+table+" WHERE userid=? ORDER BY isnews desc";
        return dataFactory.rowsWithOutObject(sql, userid);
    }
    
    /**
     * 按ID获取栏目
     * @param id
     * @param userid
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopusermenuSimpleById_${0}_${1}")
    public Map findSimpleById(int id,int userid) throws SQLException{
        String sql="SELECT id,typename FROM "+table+" WHERE id=? AND userid=?";
        return dataFactory.oneWithOutObject(sql, id,userid);
    }
    
    /**
     * 按照ID删除
     * @param id
     * @return
     * @throws SQLException 
     */
    public int DeleteById(int id) throws SQLException{
        return dataFactory.delete(table,"id=?", id);
    }
    
    /**
     * 按照ID和UserId删除
     * @param id
     * @param userid
     * @return
     * @throws SQLException 
     */
    public int DeleteByIdAndUserid(int id,int userid)throws SQLException{
        return dataFactory.delete(table,"id=? AND userid = ?",id,userid);
    }
    
    /**
     * 按照ID和userid查找栏目
     * @param id
     * @param userid
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopusermenubyidanduserid_${0}_${1}")
    public ShopUsermenu findByIdAndUserid(int id,int userid) throws SQLException{
        String sql=sqlheader+" WHERE id=? AND userid= ?";
        return dataFactory.one(ShopUsermenu.class,sql, id,userid);
    }
    
    /**
     * 插入数据
     * @param arg
     * @return
     * @throws SQLException 
     */
    public int insertBackID(ShopUsermenu arg)throws SQLException{
        return dataFactory.insertBackID(arg, table);
    }
    
}
